package com.awspaas.user.apps.cpq.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.bcel.generic.NEWARRAY;
import org.apache.poi.hslf.dev.SlideAndNotesAtomListing;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jdesktop.application.ResourceMap;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.cpq.util.RefreshDataUtils;
import com.awspaas.user.apps.cpq.util.RefreshModulDataUtils;
import com.awspaas.user.apps.cpq.util.RefreshTempDataUtils;
import com.highgo.jdbc.jdbc.FieldMetadata.Key;

import io.vavr.API.For1;

public class CPQ_RefreshModelPriceWeb {

	public String refreshModelPrice(UserContext userContext, String rowData) {
		try {
			JSONArray dataList = JSONObject.parseArray(rowData);
			String bms = "";
			// 获取需要更新的物料编号。
			Map<String, JSONObject> userMap = new HashMap<String, JSONObject>();
			// 用于存放更新后的装配件信息
			Map<String, JSONObject> zpjMap = new HashMap<String, JSONObject>();
			List<String> bhList = new ArrayList<>();
			for (int i = 0; i < dataList.size(); i++) {
				JSONObject jsonObject = (JSONObject) dataList.get(i);
				String bm = (String) jsonObject.get("BM");
				String status = (String) jsonObject.get("STATUS");
				String type = (String) jsonObject.get("TYPE");
				if (status.equals("启用")) {
					JSONObject json = new JSONObject();
					json.put("DJ", (String) jsonObject.get("DJ"));					
					json.put("CLF", (String) jsonObject.get("CLF"));					
					json.put("RGF", (String) jsonObject.get("RGF"));
					json.put("WXWG", (String) jsonObject.get("WXWG"));
					json.put("CGF", (String) jsonObject.get("CGF"));
					json.put("ZL", (String) jsonObject.get("ZL"));
					json.put("TJ", (String) jsonObject.get("TJ"));
					json.put("NAME", (String) jsonObject.get("NAME"));
					json.put("GG", (String) jsonObject.get("GG"));
					json.put("TH", (String) jsonObject.get("TH"));
					json.put("DW", (String) jsonObject.get("DW"));
					json.put("TYPE", (String) jsonObject.get("TYPE"));// 类型
					json.put("ZZFBL", (String) jsonObject.get("ZZFBL"));// 类型					
					json.put("BM", bm);
					//System.out.println("jsonObject:::::::"+jsonObject);

					//System.out.println("json:::::::"+json);
					bhList.add(bm);
					if(type.equals("装配件")) {
						json.put("ZJS", "");
						zpjMap.put(bm, json);
					}else {
						userMap.put(bm, json);
					}
					
					if (bms.isEmpty()) {
						bms = "'" + bm + "'";
					} else {
						bms += ",'" + bm + "'";
					}
				}

			}
			/**
			 * 1、获取所有状态为启用的类型为装配件物料信息BO_EU_CPQ_COMPONENT_PART
			 */

			String allCPsql = "select   a.BM,a.ID,a.LX,a.SL,a.ZL,a.TJ,"
					+ "a.CLF,"
					+ "a.RGF,a.WXWG,a.CGF,"
					+ "a.DJ,"
					+ "a.ZJ,"
					+ "a.ZCLF,"
					+ "a.ZGSF,a.ZWX,a.ZCGF,a.ZZL,a.ZTJ,"
					+ "b.NAME,b.ZZFBL,b.GG,b.TH,b.DW,b.BM as BH,b.TYPE,b.ZL as WZL,b.TJ as WTJ,"
					+ "b.DJ as JG,"
					+ "b.CLF as WCLF,"
					+ "b.RGF as WRGF,b.WXWG as WX,b.CGF as WCGF "
					+ "from  BO_EU_CPQ_COMPONENT_PART_GRID a, BO_EU_CPQ_COMPONENT_PART b "
					+ "where  a.BINDID=b.BINDID and  b.TYPE='装配件' ";
			List<RowMap> allmaps = DBSql.getMaps(allCPsql);
			// 存放需要更新的装配件信息
			Map<String, List<RowMap>> aboutCPMap = new HashMap<>();
			if (allmaps.size() > 0) {
				for (RowMap rowMap : allmaps) {
					String bh = rowMap.getString("BH");
					if (aboutCPMap.containsKey(bh)) {
						List<RowMap> lists = aboutCPMap.get(bh);
						lists.add(rowMap);
						aboutCPMap.put(bh, lists);
					} else {
						List<RowMap> lists = new ArrayList<RowMap>();
						lists.add(rowMap);
						aboutCPMap.put(bh, lists);
					}

				}
			}
			// 将需要更新的装配件进行价格更新
			String zpjsql = "update BO_EU_CPQ_COMPONENT_PART set DJ=?,"
					+ "CLF=?,"
					+ "RGF=?,WXWG=?,CGF=?,ZL=?,TJ=?,ZZF=?,DJZZF=? where BM=?";
			String zjsql = "update BO_EU_CPQ_COMPONENT_PART_GRID set DJ=?,"
					+ "CLF=?,"
					+ "RGF=?,WXWG=?,CGF=?,ZL=?,TJ=?,"
					+ "ZJ=?,"
					+ "ZCLF=?,"
					+ "ZGSF=?,ZWX=?,ZCGF=?,ZZL=?,ZTJ=?,MC=?,LX=?,GG=?,DH=?,DW=? where BM=? and  ID=?";
			// 将勾选需要更新的物料信息中的装配件进行单价更新，判断装配件中的子项是否包含勾选的物料信息中的采购件和制造件，如果包含则需要将装配件的单价进行更新后，再进行下一步操作
			/**
			 * 将更新后的勾选数据进行装配件的更新，更新完成后将装配件存放在map中，在递归将所有需要更新的装配件进行更新，将所有更新的装配件信息存放在map中。
			 */

			// 获取更新后的装配件信息
			Map<String, JSONObject> zpjNewMap = RefreshDataUtils.getZPTMaterials(aboutCPMap, zpjMap, userMap, zpjsql,
					zjsql);

			/**
			 * 将更新后的新的装配件，进行递归更新。将更新后的数据存放到map中
			 * 
			 */
			Map<String, JSONObject> map = RefreshDataUtils.getZPJMsgs(zpjNewMap, aboutCPMap, zpjsql, zjsql);
			/**
			 * 2、获取所有状态为启用的模块信息BO_EU_CPQ_MODULE_MSG
			 */
			// 将更新完成后的数据，加上勾选的数据作为参数
			for (String key : userMap.keySet()) {
				map.put(key, userMap.get(key));
			}
			Map<String, Map<String, Object>> modulLists = RefreshModulDataUtils.getModule(map);
			/**
			 * 3、获取所有状态为启用的项目报价标准模板信息BO_EU_CPQ_TEMPLATE_PRICE
			 */
			RefreshTempDataUtils.upateTemp(modulLists);
			ResponseObject ro = ResponseObject.newOkResponse();
			return ro.toString();
		} catch (Exception e) {
			throw e;
		}

	}

}
